__author__ = 'vavava'
from configparser import ConfigParser

class GripperConfig(object):
    def __init__(self,fullFileName="config.conf"):
        self.fileName=fullFileName
        self.parser = ConfigParser()

        self.host            =  "localhost"
        self.port            =  3306
        self.db              =  "wr_trace"
        self.user            =  "root"
        self.passwd          =  "root"
        self.log_config_file =  "./logger.conf"
        self.logger          =  "gripper"
        self.userTokens      =  "UCL0003192"
        self.debug_level     =  1
        self.sock_timeout    =  30        # s
        self.period          =  3600000   # ms

    def load(self,fullFileName):
        self.parser.read(fullFileName)
        print(self.parser.sections())
        try:
            host            =  self.parser.get("db","host")
            port            =  self.parser.getint("db","port")
            db              =  self.parser.get("db","db")
            user            =  self.parser.get("db","user")
            passwd          =  self.parser.get("db","passwd")
            log_config_file =  self.parser.get("log","log_config_file")
            logger          =  self.parser.get("log","logger")
            userTokens      =  self.parser.get("userToken","userTokens")
            debug_level     =  self.parser.getint("debug","debug_level")
            sock_timeout    =  self.parser.getfloat("debug","sock_timeout")
            period          =  self.parser.getint("debug","period")
        except Exception as e:
            print(e)

        if  host            != "" : self.host            = host
        if  port is not None      : self.port            = port
        if  db              != "" : self.db              = db
        if  user            != "" : self.user            = user
        if  passwd          != "" : self.passwd          = passwd
        if  log_config_file != "" : self.log_config_file = log_config_file
        if  logger          != "" : self.logger          = logger
        if  debug_level  is not None : self.debug_level  = debug_level
        if  sock_timeout is not None : self.sock_timeout = sock_timeout
        if  period       is not None : self.period       = period
        if  userTokens      != "" :
            self.userTokens = userTokens.split('|')
            for i in range(0,len(self.userTokens)):
                self.userTokens[i] = self.userTokens[i].strip()

if __name__ == "__main__":
    gc = GripperConfig()
    gc.load("gripper.conf")
    print(gc.userTokens[2])
    print(gc.userTokens)
    print(gc.debug_level)
